Skip to content

Conversation

MAJKFL
Copy link
Contributor

@MAJKFL MAJKFL commented Sep 1, 2025

This PR enables the new LICM pass to properly handle values with ownership. It adds support for the following instructions:

  • load_borrow
  • store [init]
  • load [take]
  • load [copy]
  • begin_borrow
  • store_borrow
  • begin_apply
  • store [assign]

@MAJKFL MAJKFL force-pushed the new-sil-licm-pass-copy-ownership branch from 865d153 to 9706745 Compare September 2, 2025 17:09
@MAJKFL
Copy link
Contributor Author

MAJKFL commented Sep 2, 2025

@swift-ci Please smoke test

2 similar comments
@MAJKFL
Copy link
Contributor Author

MAJKFL commented Sep 4, 2025

@swift-ci Please smoke test

@MAJKFL
Copy link
Contributor Author

MAJKFL commented Sep 5, 2025

@swift-ci Please smoke test

@MAJKFL
Copy link
Contributor Author

MAJKFL commented Sep 5, 2025

@swift-ci apple silicon benchmark

@MAJKFL
Copy link
Contributor Author

MAJKFL commented Sep 5, 2025

@swift-ci Please smoke test

@MAJKFL
Copy link
Contributor Author

MAJKFL commented Sep 5, 2025

@swift-ci apple silicon benchmark

@MAJKFL
Copy link
Contributor Author

MAJKFL commented Sep 5, 2025

@swift-ci Please test

@MAJKFL
Copy link
Contributor Author

MAJKFL commented Sep 5, 2025

@swift-ci Please Test Source Compatibility Release

@MAJKFL
Copy link
Contributor Author

MAJKFL commented Sep 8, 2025

@swift-ci Please smoke test

@MAJKFL
Copy link
Contributor Author

MAJKFL commented Sep 8, 2025

@swift-ci apple silicon benchmark

@MAJKFL
Copy link
Contributor Author

MAJKFL commented Sep 8, 2025

@swift-ci Please smoke test

@MAJKFL
Copy link
Contributor Author

MAJKFL commented Sep 8, 2025

@swift-ci apple silicon benchmark

@MAJKFL
Copy link
Contributor Author

MAJKFL commented Sep 8, 2025

@swift-ci Please Test Source Compatibility Release

Copy link
Contributor

@nate-chandler nate-chandler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

for exitBlock in loop.exitBlocks {
assert(exitBlock.hasSinglePredecessor, "Exiting edge should not be critical.")

if let createdEndBorrow {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a few more lines to clone instead of createEndBorrow for every exitBlock. Out of curiosity, why clone?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to handle multiple loop exit blocks similarly as in sink, where move is followed by copy on remaining exit blocks. Though here, I think you’re right, it’s better to just use createEndBorrow. I changed it.

_ context: FunctionPassContext
) -> Bool {
guard endInstructions.allSatisfy({ loop.contains(block: $0.parentBlock)}) else {
guard endInstructions.allSatisfy({ loop.contains(block: $0.parentBlock) && !($0 is TermInst) }) else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's an example of a TermInst that is a scope ending instruction? Do unreachables and other terminators along the availability boundary show up as scope ending users?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem here is with branching instructions with phi parameters. They can be considered as scope ending instructions, but it’s not possible to hoist them.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right. Thanks.

let exitBlocks = loop.exitBlocks
var changed = false

for exitBlock in exitBlocks {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not sink instructions into dead-end blocks? Once value lifetimes are "complete", scope-ending instructions will be required in such dead-ends. If sinking them into dead-ends is causing a problem, though, ensuring such values' lifetimes are complete can be addressed later on.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We’ve encountered quite a few bugs when trying to sink instructions into dead-ends, which I believe were primarily caused by other lifetimes not being ended properly there. I think it should be an easy fix in the future though, just removing this check.

@MAJKFL
Copy link
Contributor Author

MAJKFL commented Sep 9, 2025

@swift-ci Please smoke test

@MAJKFL
Copy link
Contributor Author

MAJKFL commented Sep 9, 2025

@swift-ci Please test

@MAJKFL MAJKFL force-pushed the new-sil-licm-pass-copy-ownership branch from 84a24a8 to 0b75a81 Compare September 10, 2025 15:28
@MAJKFL
Copy link
Contributor Author

MAJKFL commented Sep 10, 2025

@swift-ci Please smoke test

@MAJKFL
Copy link
Contributor Author

MAJKFL commented Sep 10, 2025

@swift-ci Please test

@MAJKFL MAJKFL marked this pull request as ready for review September 10, 2025 15:33
@MAJKFL MAJKFL requested a review from eeckstein as a code owner September 10, 2025 15:33
@MAJKFL
Copy link
Contributor Author

MAJKFL commented Sep 12, 2025

@swift-ci Please test macOS Platform

@AnthonyLatsis
Copy link
Collaborator

@swift-ci Please test Apple Silicon

@MAJKFL
Copy link
Contributor Author

MAJKFL commented Sep 15, 2025

@swift-ci Please smoke test

@MAJKFL
Copy link
Contributor Author

MAJKFL commented Sep 15, 2025

@swift-ci Please test

@MAJKFL MAJKFL merged commit a5c6156 into swiftlang:main Sep 16, 2025
5 checks passed
@etcwilde
Copy link
Member

Hello, this PR appears to have regressed the bootstrapping bots resulting in a compiler SIL verification failure:
https://ci.swift.org/job/swift-bootstrap-ubuntu-24_04-x86_64/452/
https://ci.swift.org/job/swift-bootstrap-amazonlinux-2023-x86_64/197/
https://ci.swift.org/view/all/job/swift-bootstrap-freebsd-14-x86_64/724/

[2025-09-16T14:43:30.877Z] <unknown>:0: warning: using (deprecated) legacy driver, Swift installation does not contain swift-driver at: '/home/build-user/stages/stage1/usr/bin/swift-driver-new'
[2025-09-16T14:43:30.877Z] <unknown>:0: warning: option '-no-emit-module-separately-wmo' is only supported in swift-driver
[2025-09-16T14:43:30.877Z] Begin Error in Function: '$ss30_copySequenceToContiguousArrayys0dE0Vy7ElementQzGxSTRzlF19CompilerSwiftParser5LexerO06LexemeB0V_Tg5'
[2025-09-16T14:43:30.877Z] Found over consume?!
[2025-09-16T14:43:30.877Z] Value:   %49 = load_borrow %46 : $*BumpPtrAllocator      // users: %69, %68
[2025-09-16T14:43:30.877Z] Block: bb1
[2025-09-16T14:43:30.877Z] Consuming Users:
[2025-09-16T14:43:30.877Z]   end_borrow %49 : $BumpPtrAllocator              // id: %69
[2025-09-16T14:43:30.877Z] 
[2025-09-16T14:43:30.877Z] End Error in Function: '$ss30_copySequenceToContiguousArrayys0dE0Vy7ElementQzGxSTRzlF19CompilerSwiftParser5LexerO06LexemeB0V_Tg5'
[2025-09-16T14:43:30.877Z] Found ownership error?!
[2025-09-16T14:43:30.877Z] <unknown>:0: error: fatal error encountered during compilation; please submit a bug report (https://swift.org/contributing/#reporting-bugs)
[2025-09-16T14:43:30.877Z] <unknown>:0: note: triggering standard assertion failure routine
...
[2025-09-16T14:43:30.877Z] 1.	Swift version 6.3-dev (LLVM 82c14639a1e4d89, Swift a5c615652529771)
[2025-09-16T14:43:30.877Z] 2.	Compiling with effective version 5.10
[2025-09-16T14:43:30.877Z] 3.	While evaluating request ExecuteSILPipelineRequest(Run pipelines { PrepareOptimizationPasses, EarlyModulePasses, HighLevel,Function+EarlyLoopOpt, HighLevel,Module+StackPromote, MidLevel,Function, ClosureSpecialize, LowLevel,Function, LateLoopOpt, SIL Debug Info Generator } on SIL for SwiftParser)
[2025-09-16T14:43:30.877Z] 4.	While running pass #8938 SILFunctionTransform "OwnershipModelEliminator" on SILFunction "@$ss30_copySequenceToContiguousArrayys0dE0Vy7ElementQzGxSTRzlF19CompilerSwiftParser5LexerO06LexemeB0V_Tg5".
[2025-09-16T14:43:30.877Z]  for <<debugloc at "<compiler-generated>":0:0>>5.	Found verification error when verifying before lowering ownership. Please re-run with -sil-verify-all to identify the actual pass that introduced the verification error.
[2025-09-16T14:43:30.877Z] 6.	While verifying SIL function "@$ss30_copySequenceToContiguousArrayys0dE0Vy7ElementQzGxSTRzlF19CompilerSwiftParser5LexerO06LexemeB0V_Tg5".
[2025-09-16T14:43:30.877Z]  for <<debugloc at "<compiler-generated>":0:0>>Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):

MAJKFL added a commit to MAJKFL/swift that referenced this pull request Sep 17, 2025
…ass-copy-ownership"

This reverts commit a5c6156, reversing
changes made to 2b6ea81.
@MAJKFL
Copy link
Contributor Author

MAJKFL commented Sep 17, 2025

Thanks. I've opened a reversion PR here: #84348

MAJKFL added a commit that referenced this pull request Sep 18, 2025
Revert "Merge pull request #84045 from MAJKFL/new-sil-licm-pass-copy-…
MAJKFL added a commit to MAJKFL/swift that referenced this pull request Sep 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants